//
//  HXSearchDetailVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/19.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXSearchDetailVC.h"
#import "HX_Macro.h"
#import "HXSearchDetailCell.h"
#import "HXSearchDetailModel.h"

@interface HXSearchDetailVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@end

@implementation HXSearchDetailVC

- (NSMutableArray *)arrDataSource {
    if (!_arrDataSource) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDETH, HEIGHT - 64) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        [_tableView registerNib:[UINib nibWithNibName:@"HXSearchDetailCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXSearchDetailCell"];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"为你推荐";
    [self getNetWorking:self.netString];
    [self.view addSubview:self.tableView];
}
- (void)getNetWorking:(NSString *)string {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"charset=UTF-8?page=1&methodName=SearchDishes&size=20&keyword=%@&version=4.40&", string];
    NSDictionary *dicHeader = @{@"Content-Tpye" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dicAll in arrData) {
            HXSearchDetailModel *search =[HXSearchDetailModel ModelWithDic:dicAll];
            [self.arrDataSource addObject:search];
        }
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXSearchDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXSearchDetailCell"];
    HXSearchDetailModel *detailModel = self.arrDataSource[indexPath.row];
    [cell.imgHeader sd_setImageWithURL:[NSURL URLWithString:detailModel.image]];
    cell.nameLabel.text = detailModel.title;
    cell.detail.text = detailModel.Description;
    cell.diffcutry.text = [NSString stringWithFormat:@"难度：%@", detailModel.hard_level];
    cell.tase.text = [NSString stringWithFormat:@"口味：%@", detailModel.taste];
    cell.creatTime.text = [NSString stringWithFormat:@"烹饪时间：%@", detailModel.cooking_time];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXSearchDetailModel *search = self.arrDataSource[indexPath.row];
    CGFloat addHeight = [SelfSizeTools selfSizeHeightWithString:search.Description Width:200 Font:[UIFont systemFontOfSize:10]];
    return 80 + addHeight;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
